#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Models/ShortRateModel.h>
#include <gen/QL/Models/AffineModel.h>
#pragma unmanaged 
#include <ql\models\shortrate\onefactormodel.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL::Math;
using namespace Cephei::QL;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
#define HANDLE
#define ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Models { namespace Shortrate {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IOneFactorAffineModel
	public ref class COneFactorAffineModel  : 
            public CAffineModel,
            public Cephei::QL::Models::Shortrate::IOneFactorAffineModel
	{
	protected: 
		boost::shared_ptr<QuantLib::OneFactorAffineModel>* _ppOneFactorAffineModel;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::OneFactorAffineModel>* _phOneFactorAffineModel;
#endif
		Object^ _OneFactorAffineModelOwner;     // reference to object that manages the storage for this object
	internal:
        COneFactorAffineModel (boost::shared_ptr<QuantLib::OneFactorAffineModel>& childNative, Object^ owner);
        COneFactorAffineModel (QuantLib::OneFactorAffineModel& childNative, Object^ owner);
        COneFactorAffineModel (COneFactorAffineModel^ copy);
        COneFactorAffineModel (PLATFORM::Type^ t);
#ifdef STRUCT
        COneFactorAffineModel (QuantLib::OneFactorAffineModel childNative);
#endif       
#ifdef HANDLE
		COneFactorAffineModel (QuantLib::Handle<QuantLib::OneFactorAffineModel>& childNative, Object^ owner);
		COneFactorAffineModel (QuantLib::Handle<QuantLib::OneFactorAffineModel> childNative);
#endif
		virtual ~COneFactorAffineModel ();
		!COneFactorAffineModel ();

	internal:
		QuantLib::OneFactorAffineModel& GetReference ();
		boost::shared_ptr<QuantLib::OneFactorAffineModel>& GetShared ();
		QuantLib::OneFactorAffineModel* GetPointer ();
        void SetOneFactorAffineModel (boost::shared_ptr<QuantLib::OneFactorAffineModel> native)
        {
            if (_ppOneFactorAffineModel != NULL)
                delete _ppOneFactorAffineModel;
            _ppOneFactorAffineModel = new boost::shared_ptr<QuantLib::OneFactorAffineModel> (native);
            SetAffineModel (boost::dynamic_pointer_cast<QuantLib::AffineModel> (*_ppOneFactorAffineModel));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::OneFactorAffineModel>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Double Discount (Double t) ;
		virtual Double DiscountBond (Double now, Double maturity, Double rate) ;
		virtual Double DiscountBond (Double now, Double maturity, Cephei::QL::Math::IArray^ factors) ;
    private:
        Cephei::QL::Models::CShortRateModel^ _ShortRateModel;
    private:
        property CShortRateModel^ ShortRateModel
        {
            CShortRateModel^ get ()
            {
                if (_ShortRateModel == nullptr)
                {
                    _ShortRateModel = REFNEW CShortRateModel (boost::dynamic_pointer_cast<QuantLib::ShortRateModel> (*_ppOneFactorAffineModel), this);
                }
                return _ShortRateModel;
            }
        }
    public:
        operator Cephei::QL::Models::IShortRateModel^ ()
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel);
        }
        operator Cephei::QL::Models::CShortRateModel^ ()
        {
            return ShortRateModel;
        }
        
    public:
        virtual Cephei::QL::ILattice^ M1576_4603 (Cephei::QL::ITimeGrid^ prm1) = Cephei::QL::Models::IShortRateModel::Tree
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Tree ( prm1);
        }
        virtual Cephei::QL::Models::ICalibratedModel^ M1576_4593 (Cephei::Core::IVector<Cephei::QL::Models::ICalibrationHelper^>^ instruments, Cephei::QL::Math::Optimization::IOptimizationMethod^ method, Cephei::QL::Math::Optimization::IEndCriteria^ endCriteria, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IConstraint^>^ constraint, Microsoft::FSharp::Core::FSharpOption<Cephei::Core::IVector<Double>^>^ weights) = Cephei::QL::Models::ICalibratedModel::Calibrate
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Calibrate ( instruments,  method,  endCriteria,  constraint,  weights);
        }
        property Cephei::QL::Math::Optimization::IConstraint^ M1576_4595 
        {
            virtual Cephei::QL::Math::Optimization::IConstraint^ get () = Cephei::QL::Models::ICalibratedModel::Constraint::get
            {
                return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Constraint;
            }
        }
        property QL::Math::Optimization::EndCriteria::TypeEnum M1576_4596 
        {
            virtual QL::Math::Optimization::EndCriteria::TypeEnum get () = Cephei::QL::Models::ICalibratedModel::EndCriteria::get
            {
                return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->EndCriteria;
            }
        }
        virtual Cephei::QL::Models::ICalibratedModel^ M1576_4599 (Cephei::QL::Math::IArray^ p_params) = Cephei::QL::Models::ICalibratedModel::SetParams
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->SetParams ( p_params);
        }
        property Cephei::QL::Models::ICalibratedModel^ M1576_4600 
        {
            virtual Cephei::QL::Models::ICalibratedModel^ get () = Cephei::QL::Models::ICalibratedModel::Update::get
            {
                return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Update;
            }
        }
        virtual Double M1576_4601 (Cephei::QL::Math::IArray^ p_params, Cephei::Core::IVector<Cephei::QL::Models::ICalibrationHelper^>^ instruments) = Cephei::QL::Models::ICalibratedModel::Value
        {
            return static_cast<Cephei::QL::Models::IShortRateModel^>(ShortRateModel)->Value ( p_params,  instruments);
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Models::Shortrate::IOneFactorAffineModel^>::typeid)]
	[FactoryFor(Cephei::QL::Models::Shortrate::IOneFactorAffineModel::typeid)]
	[FactoryFor(Cephei::QL::Models::Shortrate::IOneFactorAffineModel_Factory::typeid)]
	public ref class COneFactorAffineModel_Factory sealed : public IOneFactorAffineModel_Factory
	{
	public:
    };
   
/*Cephei*/ } /*QL*/ } /*Models*/ } /*Shortrate */}
